package net.mingsoft.flk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.base.util.JSONObject;
import net.mingsoft.basic.action.r.ManagerResponse;
import net.mingsoft.basic.biz.ILogBiz;
import net.mingsoft.basic.biz.IManagerBiz;
import net.mingsoft.basic.entity.LogEntity;
import net.mingsoft.basic.entity.ManagerEntity;
import net.mingsoft.examine.utils.BlankUtil;
import net.mingsoft.flk.entity.FlkEncryption;
import net.mingsoft.flk.entity.vo.FlkRole;
import net.mingsoft.flk.entity.vo.Role;
import net.mingsoft.flk.enums.FlkType;
import net.mingsoft.flk.service.Impl.FlkEncryptionServiceImpl;
import net.mingsoft.flk.service.Impl.RoleServiceImpl;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 雨里烤番薯
 */

@Api(tags = "flk加密校验")
@RestController
@RequestMapping("/flk")
public class FlkController {

    @Resource
    private IManagerBiz managerBiz;
    @Resource
    private RoleServiceImpl roleService;

    @Resource
    private ILogBiz logBiz;

    @Resource
    private FlkEncryptionServiceImpl encryptionService;

    @ApiOperation("登陆校验提醒")
    @PostMapping("/verifyRemind/{id}")
    @Transactional(rollbackFor = Exception.class)
    public ResultData verifyRemind(@PathVariable("id") @ApiParam("登陆用户id") String userId) {
        // 返回的文字
        String result = "";
        FlkEncryption noRemind;

        // 先获取登陆用户加密的未提醒数据
        ManagerEntity it = managerBiz.getById(Integer.parseInt(userId));
        Role role = roleService.getById(it.getRoleId());
        noRemind = encryptionService.noRemind(FlkType.USER.getCode(), userId);
        if (!BlankUtil.isBlank(noRemind)) {
            ManagerResponse managerResponse = new ManagerResponse();
            managerResponse.setId(it.getId())
                    .setAccount(it.getManagerName())
                    .setNickName(it.getManagerNickName())
                    .setRoleName(role.getRoleName())
                    .setCreatedDate(it.getCreateDate())
                    .setStatus(it.getStatus()).setDepartName(it.getDepartName()).setPost(it.getPost());
            String userJson = JSONObject.toJSONStringWithDateFormat(managerResponse, "yyyy-MM-dd HH:mm:ss");
            ResultData resultData = encryptionService.verification(noRemind.getId(), userJson);
            if (resultData.getCode() == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
                encryptionService.remind(FlkType.USER.getCode(), userId);
                result = "用户信息的完整性";
            }
        }
        //获取登陆用户的角色信息
        FlkRole flkRole = roleService.get(it.getRoleId());
        // 获取登陆用户的角色信息的未提醒数据
        noRemind = encryptionService.noRemind(FlkType.ROLE.getCode(), String.valueOf(it.getRoleId()));
        if (!BlankUtil.isBlank(noRemind)) {
            String userRoleJson = com.alibaba.fastjson.JSONObject.toJSONString(flkRole);
            ResultData resultData = encryptionService.verification(noRemind.getId(), userRoleJson);
            if (resultData.getCode() == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
                encryptionService.remind(FlkType.ROLE.getCode(), String.valueOf(it.getRoleId()));
                if (BlankUtil.isBlank(result)) {
                    result = "角色信息的完整性";
                } else {
                    result = result + ",角色信息的完整性";
                }
            }
        }
        //获取未提醒的日志信息,再根据登陆用户进行获取对应指定的日志信息
        String logResult = "";
        List<FlkEncryption> logFlkEncryption = encryptionService.list(new QueryWrapper<FlkEncryption>().eq("type", FlkType.LOG.getCode()).eq("remind", 0));
        for (FlkEncryption encryption : logFlkEncryption) {
            LogEntity logEntity = logBiz.getOne(new QueryWrapper<LogEntity>().eq("id", encryption.getBusinessId()).eq("log_user", it.getManagerName()));
            String logJson = JSONObject.toJSONStringWithDateFormat(logEntity, "yyyy-MM-dd HH:mm:ss");
            ResultData resultData = encryptionService.verification(encryption.getId(), logJson);
            if (resultData.getCode() == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
                encryptionService.remind(FlkType.ROLE.getCode(), String.valueOf(it.getRoleId()));

                logResult = "日志记录表的完整性";


            }
        }
        if (!BlankUtil.isBlank(logResult) && !BlankUtil.isBlank(result)) {
            result = result + "," + logResult;
        }
        if (BlankUtil.isBlank(result)) {
            return ResultData.build().success();
        } else {
            return ResultData.build().error("您的访问控制" + result + "已被修改，具体请联系管理员");
        }
    }

}
